#!/usr/bin/python
# Time execution of test files
from timeit import Timer
from sys import exit, argv, stderr
from fpformat import fix
import os

def run_test_jam(test_name):
	stream = os.popen("bjam -f"+test_name)
	success = False
	for line in stream.readlines():
		if line.find("**passed**")!=-1: success = True
	if not success: raise RuntimeError(test_name+" execution on BJam failed!")

def run_test_cpp(test_name):
	stream = os.popen(cpprunner+" "+test_name)
	success = False
	for line in stream.readlines():
		if line.find("**passed**")!=-1: success = True
	if not success: raise RuntimeError(test_name+" execution on BJamC++ failed!")

cpprunner = os.path.abspath(argv[1]).replace("\\","/")
test = os.path.abspath(argv[2]).replace("\\","/")
test_orig = argv[2]

try:
	t = Timer(setup='from __main__ import run_test_cpp', stmt='run_test_cpp("'+test+'")');
	cpptime=t.timeit(1)

	t = Timer(setup='from __main__ import run_test_jam', stmt='run_test_jam("'+test+'")');
	jamtime=t.timeit(1)

	percent=cpptime/jamtime*100.0
	msg="'"+test_orig+"': "
	msg+=fix(jamtime,3)+" - BJam time, "
	msg+=fix(cpptime,3)+" - BJamC++ time "
	msg+="("+fix(percent,3)+"%)."
	if (jamtime*1.1)<cpptime:
		stderr.write ("Regression: "+msg)
		exit(2)
	else:
		print "OK: "+msg
except RuntimeError, err:
	print err
	exit(1)
